package cn.tedu.egweibo0416.controller;

import cn.tedu.egweibo0416.base.response.JsonResult;
import cn.tedu.egweibo0416.mapper.CommentMapper;
import cn.tedu.egweibo0416.pojo.dto.CommentAddParam;
import cn.tedu.egweibo0416.pojo.entity.Comment;
import cn.tedu.egweibo0416.pojo.vo.CommentVO;
import cn.tedu.egweibo0416.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Validated
@Api(tags = "03.微博模块")
@RestController
@RequestMapping("/v1/comment/")
@Slf4j
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @ApiOperation("发表评论功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("insert")
    public JsonResult addComment(@RequestBody CommentAddParam commentAddParam, @ApiIgnore HttpSession session){
        log.debug("commentAddParam = " + commentAddParam);
        UserVO userVO = (UserVO) session.getAttribute("user");
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddParam, comment);
        comment.setCreateTime(new Date());
        comment.setUserId(userVO.getId());
        log.debug("comment = " + comment);
        commentMapper.insertComment(comment);
        return JsonResult.ok();
    }

    @ApiOperation("获取评论列表功能")
    @ApiOperationSupport(order = 20)
    @GetMapping("selectByWeiboId")
    public JsonResult getWeiboList(@Range(min = 100, max = 500, message = "微博编号必须在100-500之间") @RequestParam Long id){
        log.debug("id = " + id);
        List<CommentVO> list = commentMapper.selectByWeiboId(id);
        return JsonResult.ok(list);
    }
}
